﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Configuration;

namespace de.rwth.rz.campus.labs.Mailer2010
{
    public class CarbonCopyField : SPFieldText
    {
        private static Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);

        public CarbonCopyField(SPFieldCollection fields, string fieldName) :

            base(fields, fieldName) { }

        public CarbonCopyField(SPFieldCollection fields, string typeName, string displayName) :

            base(fields, typeName, displayName) { }

        // Validate the string. If not valid, throw an SPFieldValidationException
        public override string GetValidatedString(object value)
        {
            if (!string.IsNullOrEmpty(value.ToString()))
            {
                string[] sArr = value.ToString().Split(new char[] { ';' });
                foreach (string s in sArr)
                {
                    if (!string.IsNullOrEmpty(s.Trim()) && !regex.IsMatch(s.Trim()))
                        throw new SPFieldValidationException("Dieses Feld enthält mindestens eine ungültige E-Mailadresse.");
                }
            }
            return base.GetValidatedString(value);
        }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl control = new CarbonCopyFieldControl();
                control.FieldName = this.InternalName;
                return control;
            }
        }

    }

    public class CarbonCopyFieldControl : BaseFieldControl
    {
        protected InputFormTextBox CarbonCopyField;
        protected Literal literal;

        protected override string DefaultTemplateName
        {
            get
            {
                return @"CarbonCopyFieldControl";
            }
        }

        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                return this.CarbonCopyField.Text;
            }
            set
            {
                EnsureChildControls();
                this.CarbonCopyField.Text = (string)this.ItemFieldValue;
            }
        }

        protected override void CreateChildControls()
        {
            if (this.Field == null || this.ControlMode == SPControlMode.Display)
                return;
            base.CreateChildControls();

            this.CarbonCopyField = (InputFormTextBox)TemplateContainer.FindControl("CarbonCopy");
            this.literal = (Literal)TemplateContainer.FindControl("carbon_description");

            if (this.CarbonCopyField == null)
                throw new ConfigurationErrorsException("Corrupted CarbonCopyField.ascx file.");
            if (!Page.IsPostBack)
            {
                literal.Text = SPUtility.GetLocalizedString("$Resources:Mailer,CcExample", "Mailer", SPContext.Current.Web.Language);
            }
        }

    }
}
